clear all
set printcolor gs1, permanently
set more off
global dirout ""
global dirgeolocalized ""
global dirobs ""
global dirDENUE ""

cd "$dirobs"

use "eic_2015.dta", clear
gen school_public=0 if goes_school==1
replace school_public=1 if school_transp_bus==1 | school_transp_metro==1

gen work_public=0 if goes_work==1
replace work_public=1 if work_transp_bus==1 | work_transp_metro==1

replace income=. if goes_work==0
collapse (mean) income school_public work_public [w=factor], by(state municipio)
rename state CVE_ENT
rename municipio CVE_MUN
save "TEMP_EIC.dta", replace



use "BDIF.dta", clear
keep if year==2017
keep state municipio credit debit atm_trans population branches atms adults
collapse (sum) debit credit atm_trans population branches atms adults, by(state municipio)
rename state CVE_ENT
rename municipio CVE_MUN
save "TEMP_BDIF.dta", replace


use "$dirgeolocalized/collapsed_uber_censo_lessthan500mts.dta", clear
keep year fares_card fares_cash CVE_ENT CVE_MUN POBTOT trips_card trips_cash NOM_MUN
keep if year==2017
keep if CVE_ENT=="15"
egen trips=rowtotal(trips_card trips_cash)
egen fares=rowtotal(fares_card fares_cash)
replace fares_cash=0 if fares_cash==.
collapse (sum) trips fares fares_cash POBTOT, by(CVE_ENT CVE_MUN NOM_MUN)
gen share_cash=fares_cash/fares

merge 1:1 CVE_ENT CVE_MUN using "TEMP_EIC.dta", nogenerate keep(match)
merge 1:1 CVE_ENT CVE_MUN using "TEMP_BDIF.dta", nogenerate keep(match)

gen debit_p=(debit/population)
gen credit_p=(credit/population)
gen atm_trans_p=(atm_trans/population)
gen branches_p=(branches/population)*1000
gen atms_p=(atms/population)*1000

gen debit_a=(debit/adults)
gen credit_a=(credit/adults)
gen atm_trans_a=(atm_trans/adults)
gen branches_a=(branches/adults)*1000
gen atms_a=(atms/adults)*1000

gen income_usd=income/17.2
		
		
		#delimit
		twoway 
		(scatter share_cash income_usd if trips>4, mcolor(ebblue) mlabel(NOM_MUN))
		(lfit share_cash income_usd if trips>4, lcolor(navy) lwidth(thick))
		, ytitle("Share of cash", height(5)) title(" ") 
		xtitle("Average income per capita - monthly (USD)") ylabel(0[.2].8)
		legend(off) graphregion(color(white)) plotregion(fcolor(white));
		#delimit cr	
		graph export "$dirout/FigureC3a.eps", replace
		
		#delimit
		twoway 
		(scatter share_cash debit_a if trips>4, mcolor(ebblue) mlabel(NOM_MUN))
		(lfit share_cash debit_a if trips>4, lcolor(navy) lwidth(thick))
		, ytitle("Share of cash", height(5)) title(" ") 
		xtitle("Debit cards per capita")  xlabel(0[.5]2) ylabel(0[.2].8)
		legend(off) graphregion(color(white)) plotregion(fcolor(white));
		#delimit cr	
		graph export "$dirout/FigureC5a.eps", replace
		
		#delimit
		twoway 
		(scatter share_cash credit_p if trips>4, mcolor(ebblue) mlabel(NOM_MUN))
		(lfit share_cash credit_p if trips>4, lcolor(navy) lwidth(thick))
		, ytitle("Share of cash", height(5)) title(" ") 
		xtitle("Credit cards per capita") xlabel(0[.1].5) ylabel(0[.2].8)
		legend(off) graphregion(color(white)) plotregion(fcolor(white));
		#delimit cr	
		graph export "$dirout/FigureC5b.eps", replace
		
		#delimit
		twoway 
		(scatter share_cash atm_trans_p if trips>4, mcolor(ebblue) mlabel(NOM_MUN))
		(lfit share_cash atm_trans_p if trips>4, lcolor(navy) lwidth(thick))
		, ytitle("Share of cash", height(5)) title(" ") 
		xtitle("Bank branches per 1000 people") xlabel(0[.5]3) ylabel(0[.2].8)
		legend(off) graphregion(color(white)) plotregion(fcolor(white));
		#delimit cr	
		graph export "$dirout/FigureC5c.eps", replace
		
		#delimit
		twoway 
		(scatter share_cash atms_p if trips>4, mcolor(ebblue) mlabel(NOM_MUN))
		(lfit share_cash atms_p if trips>4, lcolor(navy) lwidth(thick))
		, ytitle("Share of cash", height(5)) title(" ") 
		xtitle("ATMs per 1000 people") xlabel(0[.2].8) ylabel(0[.2].8)
		legend(off) graphregion(color(white)) plotregion(fcolor(white));
		#delimit cr	
		graph export "$dirout/FigureC5d.eps", replace


capture erase "TEMP_BDIF.dta"
capture erase "TEMP_EIC.dta"





import delimited "$dirDENUE/conjunto_de_datos/denue_inegi_52_.csv", encoding(ISO-8859-1) clear

keep if nombre_act=="Banca mÃºltiple" | nombre_act=="Otras instituciones de intermediaciÃ³n crediticia y financiera no bursÃ¡til" ///
| nombre_act=="Cajas de ahorro popular" ///
| nombre_act=="Servicios relacionados con la intermediaciÃ³n crediticia no bursÃ¡til" ///
| nombre_act=="Otras instituciones de ahorro y prÃ©stamo" ///
| nombre_act=="Banca mÃºltiple" /// 
| nombre_act=="Banca de desarrollo" /// 
| nombre_act=="Uniones de crÃ©dito"

*keep if nombre_act=="Banca mÃºltiple"

gen count_banks=1
collapse (sum) count, by(manzana ageb cve_loc cve_mun cve_ent)

gen str3 CVE_MZA = string(manzana,"%03.0f")
rename ageb CVE_AGEB 
gen str4 CVE_LOC = string(cve_loc,"%04.0f")
gen str3 CVE_MUN = string(cve_mun,"%03.0f")
gen str2 CVE_ENT = string(cve_ent,"%02.0f")
keep CVE* count
save "$dirgeolocalized/denue_bancos.dta", replace
collapse (sum) count, by(CVE_ENT CVE_MUN CVE_LOC CVE_AGEB)
save "$dirgeolocalized/denue_bancos_ageb.dta", replace



use "$dirgeolocalized/collapsed_uber_censo_lessthan500mts.dta", clear
keep year ID trips_card fares_card trips_cash fares_cash ///
CVE_ENT CVE_MUN CVE_LOC CVE_AGEB CVE_MZA POBTOT VIVTOT /// 
keep if POBTOT>0
keep if VIVTOT>0
drop if year==2018
egen trips=rowtotal(trips_card trips_cash)
egen fares=rowtotal(fares_card fares_cash)
replace trips_cash=0 if trips_cash==.
replace fares_cash=0 if fares_cash==.
gen share_cash=fares_cash/fares
tsset ID year 
tsfill, full 
by ID: carryforward CVE_ENT CVE_MUN CVE_LOC CVE_AGEB CVE_MZA, replace

keep if year==2017

merge 1:1 CVE_ENT CVE_MUN CVE_LOC CVE_AGEB CVE_MZA using "$dirgeolocalized/denue_bancos.dta"

drop if _merge==2
drop _merge
replace count_banks=0 if count_banks==.

		binscatter count_banks share_cash , ///
		nq(100) linetype(lfit) lcolor(navy) mcolor(ebblue) ///
		xtitle("Share of trips paid in cash") ///
		ytitle(Probability of a retail bank in the census block) ylabel(0[.04].12) savegraph("$dirout/FigureC6.eps") replace	
		
		
		
		

		
		
